package com.bocai.kelalacar.ui.car.order.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.bean.MyGradOrderBean;
import com.bocai.kelalacar.bean.MyPublishStrokeBean;
import com.bocai.kelalacar.bean.OrderDetailBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.order.contract.MyOrderContract;
import com.bocai.kelalacar.ui.car.order.module.MyOrderModule;
import com.bocai.kelalacar.ui.car.order.presenter.MyOrderPresenter;
import com.bocai.kelalacar.ui.car.set.act.CarManageActivity;
import com.bocai.kelalacar.ui.car.set.act.DriverMangerActivity;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.widget.SpinnerDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/7/6.
 */

public class ChangeCarAndDriverActivity extends BaseActivity implements MyOrderContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_select_car)
    TextView tvSelectCar;
    @BindView(R.id.ll_select_car)
    LinearLayout llSelectCar;
    @BindView(R.id.tv_select_driver)
    TextView tvSelectDriver;
    @BindView(R.id.ll_select_driver)
    LinearLayout llSelectDriver;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @Inject
    MyOrderPresenter presenter;
    List<CarDriverBean.ContentBean.CarListBean> carListBeen = new ArrayList<>();
    List<CarDriverBean.ContentBean.DriverListBean> driverListBeen = new ArrayList<>();
    String carId;
    String driverId;
    String orderId;
    @BindView(R.id.btn)
    Button btn;

    @Override
    public int getLayoutId() {
        return R.layout.act_change_car_and_driver;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new MyOrderModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("变更车辆司机");
        initBack(imgBack, llBack);
        orderId = getIntent().getStringExtra("orderId");
        presenter.getCarDriverList(SP.get(this, "id", "").toString());
    }

    @OnClick({R.id.ll_select_car, R.id.ll_select_driver, R.id.btn, R.id.right_btn})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.ll_select_car:
                new SpinnerDialog(this).builder().setCarList(carListBeen, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0) {

                        } else if (position == carListBeen.size() + 1) {
                            startActivityForResult(new Intent(ChangeCarAndDriverActivity.this, CarManageActivity.class), 234);
//                            startActivity(AddCarActivity.class);
                        } else {
                            position = position - 1;
                            carId = carListBeen.get(position).getCarId();
                            tvSelectCar.setText(carListBeen.get(position).getCarLicense() + "   " + carListBeen.get(position).getSeats() + "座");
                            if (!TextUtils.isEmpty(carId) && !TextUtils.isEmpty(driverId)) {
                                btn.setEnabled(true);
                                btn.setTextColor(getResources().getColor(R.color.color_white));
                            }else {
                                btn.setEnabled(false);
                                btn.setTextColor(getResources().getColor(R.color.text_color));
                            }
                        }
                    }
                }).show();
                break;
            case R.id.ll_select_driver:
                new SpinnerDialog(this).builder().setDriverList(driverListBeen, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0) {

                        } else if (position == driverListBeen.size() + 1) {
                            startActivityForResult(new Intent(ChangeCarAndDriverActivity.this, DriverMangerActivity.class), 234);
//                            startActivity(AddDriverActivity.class);
                        } else {
                            position = position - 1;
                            driverId = driverListBeen.get(position).getDriverId();
                            tvSelectDriver.setText(driverListBeen.get(position).getDriverName());
                            if (!TextUtils.isEmpty(carId) && !TextUtils.isEmpty(driverId)) {
                                btn.setEnabled(true);
                                btn.setTextColor(getResources().getColor(R.color.color_white));
                            }else {
                                btn.setEnabled(false);
                                btn.setTextColor(getResources().getColor(R.color.text_color));
                            }
                        }
                    }
                }).show();
                break;
            case R.id.btn:
                if (TextUtils.isEmpty(carId)) {
                    showErrorTip("请选择出发车辆");
                } else if (TextUtils.isEmpty(driverId)) {
                    showErrorTip("请选择出发司机");
                } else {
                    presenter.teamArrange(orderId, carId, driverId);
                }
                break;
        }
    }

    @Override
    public void myStrokePublish(MyPublishStrokeBean bean) {

    }

    @Override
    public void myGradOrder(MyGradOrderBean bean) {

    }

    @Override
    public void getOrderDetail(OrderDetailBean bean) {

    }

    @Override
    public void getCarDriverList(CarDriverBean bean) {
        carListBeen = bean.getContent().getCarList();
        driverListBeen = bean.getContent().getDriverList();
    }

    @Override
    public void teamArrangeSuccess() {
        showErrorTip("修改成功");
        setResult(345);
        finish();
    }

    @Override
    public void startStrokeSuccess() {

    }

    @Override
    public void cancelOrderSuccess() {

    }

    @Override
    public void finishOrderSuccess() {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 234){
            presenter.getCarDriverList(SP.get(this, "id", "").toString());
        }
    }
}
